//
//  PersonalController.m
//  BalanceCar
//
//  Created by 郑信鸿 on 17/6/27.
//  Copyright © 2017年 郑信鸿. All rights reserved.
//

#import "PersonalController.h"
#import "CarInfoController.h"
#import "ItemCellView.h"
#import "SeparateLineView.h"
#import "UIView+shadow.h"
#import "BaseSendData.h"
#import "CarTravelModel.h"
#import "ChangePasswordController.h"

@interface PersonalController ()<ItemCellViewDelegate>

@property(nonatomic, strong)UILabel *nameLabel;

@property(nonatomic, weak)UIView *topView;

@property(nonatomic, weak)UIView *bottomView;

@property(nonatomic, weak)UIView *centerView;

@property(nonatomic, strong)NSMutableArray *centerItems;

@property(nonatomic, strong)UISwitch *navSwitch;//巡航开关

@property(nonatomic, strong)UISwitch *assinSwith;//助力开关

@property(nonatomic, assign) CGFloat viewHeight;

@property(nonatomic, assign)CGFloat viewGap;

@property(nonatomic, assign)CGFloat itemHeight;

@end

@implementation PersonalController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = XHLocalizedString(@"User information");
    self.centerItems = [NSMutableArray array];
    self.itemHeight = 30;
    self.viewGap = 15;
    
    if (kUIScreenWidth == 320) {
        
        self.viewGap = 10;
        
    }
    self.viewHeight = self.itemHeight * 3 + self.viewGap * 6;
    // Do any additional setup after loading the view.
    [self bulidTopView];
    [self bulidCenterView];
    [self bulidBottomView];
    [self assinTravelModel];
  
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:XHLocalizedString(@"To mile") forState:UIControlStateNormal];
    [button setTitle:XHLocalizedString(@"To kilometer") forState:UIControlStateSelected];
    [button setTitleColor:RGBColor(102, 102, 102) forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 80, 30);
    button.layer.borderWidth = 0.5;
    button.layer.borderColor = RGBColor(102, 102, 102).CGColor;
    button.layer.cornerRadius = 2.0;
    button.layer.masksToBounds = YES;
    button.selected = [SwitcherTool shareManager].kmOrMile;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(travelDetail:) name:kPeripheraDidReadDataNotification object:nil];
    
    [BaseSendData sendTravelParamsOrder];
   // [self assinTravelModel];
    
}

- (void)buttonAction:(UIButton *)button{
    
    button.selected = !button.selected;
    [SwitcherTool shareManager].kmOrMile = button.selected;
    
}

- (void)bulidTopView{
    
    UIView *topView = [[UIView alloc] init];
    topView.backgroundColor = [UIColor whiteColor];
    [topView addShadowToView];
    [self.view addSubview:topView];
    self.topView = topView;
    CGFloat gap = 12;
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(gap);
        make.left.mas_offset(gap);
        make.right.mas_offset(-gap);
        make.height.mas_offset(146);
    }];
    UIImageView *headImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myinfo_profile"]];
    [topView addSubview:headImage];
    [headImage mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_offset(18);
        make.centerX.mas_offset(0);
    }];
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.text = [NSString stringWithFormat:@"%@",[BalanceBuleManager shareManager].connectPeripheral.name];
    self.nameLabel.textColor = [UIColor colorFromHexCode:@"#666666"];
    self.nameLabel.font = [UIFont systemFontOfSize:16];
    [topView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(headImage.mas_bottom).offset(gap);
        make.centerX.mas_offset(0);
    }];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(travelDetail:) name:kPeripheraDidReadDataNotification object:nil];
}


- (void)bulidCenterView{
   
  
    UIView *centerView = [[UIView alloc] init];
    centerView.backgroundColor = [UIColor whiteColor];
    [centerView addShadowToView];
    [self.view addSubview:centerView];
    self.centerView = centerView;
    CGFloat gap = 12;
    [centerView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.topView.mas_bottom).offset(gap);
        make.left.mas_offset(gap);
        make.right.mas_offset(-gap);
        make.height.mas_offset(self.viewHeight);
        
    }];
    NSArray *imageNames = @[@"myinfo_listicon1",@"myinfo_listicon2",@"myinfo_listicon3"];
    NSArray *titles = @[XHLocalizedString(@"My scooter"),XHLocalizedString(@"Assistance"),XHLocalizedString(@"Cruse")];
    for (int i = 0; i < titles.count; i++) {
        NSString *imageName = imageNames[i];
        NSString *title = titles[i];
        ItemCellView *itemView = [[ItemCellView alloc] initWithImageName:imageName title:title];
        [centerView addSubview:itemView];
        [self.centerItems addObject:itemView];
    }
    
    self.navSwitch = [[UISwitch alloc] init];
    [self.navSwitch addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
    self.navSwitch.tag = 101;
    ItemCellView *navItem = self.centerItems[2];
    [navItem addSubview:self.navSwitch];
    [self.navSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_offset(0);
        
        make.right.mas_offset(0);
        
    }];
    
    self.assinSwith = [[UISwitch alloc] init];
    [self.assinSwith addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
    self.assinSwith.tag = 100;
    ItemCellView *assiItem = self.centerItems[1];
    [assiItem addSubview:self.assinSwith];
    
    [self.assinSwith mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_offset(0);
        
        make.right.mas_offset(0);
        
    }];
    
    [self.centerItems mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_offset(16);
        make.height.mas_offset(self.itemHeight);
        make.right.mas_offset(-16);
        
    }];
    [self.centerItems mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:self.viewGap * 2 leadSpacing:self.viewGap tailSpacing:self.viewGap];
    ItemCellView *itemView1 = self.centerItems[0];
    itemView1.tag = 100;
    itemView1.arrowIcon.hidden = NO;
    itemView1.delegate = self;
    
    SeparateLineView *line1 = [[SeparateLineView alloc] init];
    [centerView addSubview:line1];
    SeparateLineView *line2 = [[SeparateLineView alloc] init];
    [centerView addSubview:line2];
    NSArray *lines = @[line1,line2];
    [lines mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.height.mas_offset(0.5);
        make.left.right.mas_offset(0);
    }];
    [lines mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedItemLength:0.5 leadSpacing:self.viewGap * 2 + self.itemHeight tailSpacing:self.viewGap * 2 + self.itemHeight];
}

- (void)bulidBottomView{
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = [UIColor whiteColor];
    [bottomView addShadowToView];
    [self.view addSubview:bottomView];
    self.bottomView = bottomView;
    CGFloat gap = 12;
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.centerView.mas_bottom).offset(gap);
        make.left.mas_offset(gap);
        make.right.mas_offset(-gap);
        make.height.mas_offset(self.viewHeight);
    }];
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    ItemCellView *itemView1 = [[ItemCellView alloc] initWithImageName:@"" title:XHLocalizedString(@"Current version")];
    itemView1.valueLabel.text = app_Version;
    [bottomView addSubview:itemView1];
    ItemCellView *itemView2 = [[ItemCellView alloc] initWithImageName:@"" title:XHLocalizedString(@"Report the problem")];
    [bottomView addSubview:itemView2];
    ItemCellView *itemView3 = [[ItemCellView alloc] initWithImageName:@"" title:XHLocalizedString(@"Change password")];
    itemView3.delegate = self;
    itemView3.arrowIcon.hidden = NO;
    [bottomView addSubview:itemView3];
    itemView3.tag = 105;
    NSArray *items = @[itemView1,itemView2, itemView3];
    [items mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(16);
        make.right.mas_offset(-16);
    }];
   [items mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:self.viewGap * 2 leadSpacing:self.viewGap tailSpacing:self.viewGap];
    SeparateLineView *line1 = [[SeparateLineView alloc] init];
    [bottomView addSubview:line1];
    SeparateLineView *line2 = [[SeparateLineView alloc] init];
    [bottomView addSubview:line2];
    NSArray *lines = @[line1,line2];
    [lines mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_offset(0.5);
        make.left.right.mas_offset(0);
    }];
   [lines mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedItemLength:0.5 leadSpacing:self.viewGap * 2 + self.itemHeight tailSpacing:self.viewGap * 2 + self.itemHeight];
    
}

#pragma delegate

- (void)itemCellView:(ItemCellView *)itemView touchInWtihValue:(id)value{
    switch (itemView.tag) {
        case 100:
        {
            CarInfoController *carInfo = [[CarInfoController alloc] init];
            
            [self.navigationController pushViewController:carInfo animated:YES];
            
        }
            break;
        case 105:{
            
            ChangePasswordController *changePassword = [[ChangePasswordController alloc] init];
            
            [self.navigationController pushViewController:changePassword animated:YES];
            
        }
            break;
        default:
            break;
    }
    
    
}


- (void)switchChange:(UISwitch *)OnOff{
    
    if (OnOff.tag == 100) {
        //[LTCSingleHud showLoading:nil];
        [[BaseSendData shareManager] sendIsAssistance:OnOff.on complete:^(NSInteger code) {
            
            if (code == 100) {
                return ;
            }
            //[LTCSingleHud hideHud];
            if (code == 1) {
                
            }else{
                 OnOff.on = !OnOff.on;
               // [LTCSingleHud showMessage:@"操作失败"];
            }
            
        }];
        
    }
    if (OnOff.tag == 101) {
        
        [[BaseSendData shareManager] sendIsNavigate:OnOff.on complete:^(NSInteger code) {
            
            if (code == 100) {
                return ;
            }
           // [LTCSingleHud hideHud];
            if (code == 1) {
                
               
                
            }else{
                 OnOff.on = !OnOff.on;
                //[LTCSingleHud showMessage:@"操作失败"];
            }
            
        }];
        
    }
    
    
}

- (void)travelDetail:(NSNotification *)notice{
    
    ResultModel *model = notice.object;
    if (model.dataType == TravelParamType) {
        
        [self assinTravelModel];
        
    }
    
}


- (void)assinTravelModel{
    
    self.navSwitch.on = [BaseAcceptData shareManager].travelModel.isXH;
    self.assinSwith.on = [BaseAcceptData shareManager].travelModel.mode;
    
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
